using Microsoft.EntityFrameworkCore;
using UwinEducation.Domain.Entities;
using UwinEducation.Domain.IRepositories;
using UwinEducation.Infrastructure.Persistence;
using UwinEducation.Shared.Enums;
using UwinEducation.Shared.Models;

namespace UwinEducation.Infrastructure.Repositories
{
    /// <summary>
    /// 学生缴费仓储实现
    /// </summary>
    public class StudentPaymentRepository : Repository<StudentPayment, Guid>, IStudentPaymentRepository
    {
        public StudentPaymentRepository(ApplicationDbContext context) : base(context)
        {
        }

        /// <summary>
        /// 获取学生缴费分页列表
        /// </summary>
        public async Task<PagedResult<StudentPayment>> GetPagedListAsync(StudentPaymentPageRequest pageRequest)
        {
            var query = _context.StudentPayments
                .Include(sp => sp.Student)
                .AsQueryable();

            if (pageRequest.StudentId.HasValue)
            {
                query = query.Where(sp => sp.StudentId == pageRequest.StudentId);
            }

            if (pageRequest.TeacherScheduleGroupId.HasValue)
            {
                query = query.Where(sp => sp.TeacherScheduleGroupId == pageRequest.TeacherScheduleGroupId);
            }

            if (pageRequest.PaymentStatus.HasValue)
            {
                query = query.Where(sp => sp.Status == (PaymentStatus)pageRequest.PaymentStatus.Value);
            }

            var total = await query.CountAsync();
            var items = await query
                .OrderByDescending(sp => sp.CreatedAt)
                .Skip((pageRequest.PageIndex - 1) * pageRequest.PageSize)
                .Take(pageRequest.PageSize)
                .ToListAsync();

            return new PagedResult<StudentPayment>
            {
                Total = total,
                Items = items
            };

        }
        /// <summary>
        /// 获取学生缴费详情
        /// </summary>
        public async Task<StudentPayment> GetDetailsByIdAsync(Guid id)
        {

            var query = _context.StudentPayments
                .Include(sp => sp.Student)
                .AsQueryable();

            return await query.FirstOrDefaultAsync(sp => sp.Id == id);  

        }

        /// <summary>
        /// 获取学生的所有缴费记录
        /// </summary>
        public async Task<List<StudentPayment>> GetByStudentIdAsync(Guid studentId)
        {
            var query = _context.StudentPayments
                .Include(sp => sp.Student)
                .AsQueryable();

            return await query.OrderByDescending(sp => sp.CreatedAt).ToListAsync();

        }

        // /// <summary>
        // /// 批量新增学生缴费记录
        // /// </summary>
        // public override async Task AddRangeAsync(List<StudentPayment> payments)
        // {
        //     await _context.StudentPayments.AddRangeAsync(payments);
        //     await _context.SaveChangesAsync();
        // }

        /// <summary>
        /// 根据学生ID和教师课表组ID获取学生缴费记录
        /// </summary>
        public async Task<List<StudentPayment>> GetByStudentIdsAndTeacherScheduleGroupIdsAsync(List<Guid> studentIds, List<Guid> teacherScheduleGroupIds)
        {
            return await _context.StudentPayments.Where(sp => studentIds.Contains(sp.StudentId) && teacherScheduleGroupIds.Contains(sp.TeacherScheduleGroupId)).ToListAsync();
        }

        /// <summary>
        /// 获取查询对象
        /// </summary>
        public IQueryable<StudentPayment> GetQuery()
        {
            return _context.StudentPayments;
        }

        /// <summary>
        /// 根据学生ID和教师课表组ID获取学生缴费记录
        /// </summary>
        public async Task<StudentPayment> GetByStudentAndTeacherScheduleAsync(Guid studentId, Guid teacherScheduleGroupId)
        {
            return await _context.StudentPayments.FirstOrDefaultAsync(sp => sp.StudentId == studentId && sp.TeacherScheduleGroupId == teacherScheduleGroupId);
        }

        //DeleteByStudentIdAndTeacherScheduleIdAsync
        public async Task DeleteByStudentIdAndTeacherScheduleIdAsync(Guid studentId, Guid teacherScheduleId)
        {
            //批量逻辑删除

            var payments = await _context.StudentPayments.Where(sp => sp.StudentId == studentId && sp.TeacherScheduleGroupId == teacherScheduleId).ToListAsync();
            foreach (var payment in payments)
            {
                payment.IsDeleted = true;
            }
            _context.StudentPayments.UpdateRange(payments);
            await _context.SaveChangesAsync();
        }




    }
}